package br.ufal.tci.nexos.arcolive.tests;

import java.beans.Introspector;
import java.net.URL;
import java.util.Vector;

import javax.media.protocol.DataSource;

import br.ufal.tci.nexos.arcolive.beans.Connection;
import br.ufal.tci.nexos.arcolive.beans.ArCoLIVEPlayer;
import br.ufal.tci.nexos.arcolive.beans.CannotOpenConnectionException;

/**
 * LVSPlayer.java
 * 
 * <p>
 * This class is a implementation of Player Interface.
 * </p>
 * 
 * 
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de
 *         Sales</a>.
 * @since 0.1
 * @version 0.1
 * 
 * <p>
 * <b>Revisions:</b>
 * 
 * <p>
 * <b>20050317 leandro e felipe</b>
 * <ul>
 * <li> First version
 * </ul>
 */

public class LVSNetworkPlayer extends ArCoLIVEPlayer /*implements
		ArCoLIVERTPStreamHandlerListener, Serializable*/ {

	private DataSource datasource;

	private Vector playerStreamListeners;

	private URL source;

	private String path;

	private Connection tcpClient;

	private LVSNetworkPlayer lvsRtpHandler;

	public LVSNetworkPlayer() {
		String[] beanInfosPath = { "br.ufal.tci.arcolive.beans.info" };
		Introspector.setBeanInfoSearchPath(beanInfosPath);
		this.tcpClient = new Connection();
		this.playerStreamListeners = new Vector();
	}

	public void connect(String host, int port) {
		tcpClient.setRemoteHostProperty(host);
		tcpClient.setRemotePortProperty(port);
		try {
			tcpClient.open();
		} catch (CannotOpenConnectionException e) {
			e.printStackTrace();
		}
	}

/*	public void receiveSocketEvent(ArCoLIVEConnectionEvent event) {

		String serverMessage = event.getMessage();

		LVSControlConnectionProcessResult processResult = LVSProtocol
				.processInput(serverMessage);

		switch (processResult.getId()) {
		case LVSControlConnectionProtocol.CONNECTED:
			this.tcpClient.sendLn(LVSControlConnectionProtocol.encodeMessage(
					LVSConferenceProtocol.GETSTREAMURL, false));
			break;

		case LVSConferenceProtocol.GETSTREAMURL:
			this.lvsRtpHandler = new ArCoLIVERTPStreamHandler();
			this.lvsRtpHandler.addLvsRtpStreamListener(this);
			this.lvsRtpHandler.handle((String) processResult
					.getParameter("url"), Integer
					.parseInt((String) processResult.getParameter("portRTP")));
			break;
		// case LVSStreamProtocol.PRTICIPANTLEAVE:
		// this.goParticipantPart();
		}

	}

	public ArCoLIVEDatasource getDatasource() throws MalformedURLException {
		LVSMediaLocator lvsMediaLocator = new LVSMediaLocator(this.path);
		return new ArCoLIVEDatasource(lvsMediaLocator);
	}

	public void stopReceiveStreamEvent(
			ArCoLIVERTPStreamHandlerEvent lvsRtpStreamEvent) {
		this.notifyParticipantLeaveEvent();
	}

	public void playerReadyEvent(ArCoLIVERTPStreamHandlerEvent lvsRtpStreamEvent) {
		this.notifyPlayerReadyEvent();

	}

	public synchronized void addLvsPlayerStreamListener(
			LVSPlayerStreamListener lvsPlayerStreamListener) {
		if (!this.playerStreamListeners.contains(lvsPlayerStreamListener)) {
			this.playerStreamListeners.addElement(lvsPlayerStreamListener);
		}
	}

	public synchronized void removeLvsPlayerStreamListener(
			LVSPlayerStreamListener lvsPlayerStreamListener) {
		this.playerStreamListeners.remove(lvsPlayerStreamListener);
	}

	public synchronized void notifyPlayerReadyEvent() {
		Vector currentListeners = null;
		currentListeners = (Vector) this.playerStreamListeners.clone();
		LVSPlayerStreamEvent lvsPlayerStreamEvent = new LVSPlayerStreamEvent(
				this);
		int size = playerStreamListeners.size();
		for (int i = 0; i < size; i++) {
			((LVSPlayerStreamListener) currentListeners.elementAt(i))
					.playerReadyEvent(lvsPlayerStreamEvent);
		}
	}

	public synchronized void notifyParticipantLeaveEvent() {
		Vector currentListeners = null;
		currentListeners = (Vector) this.playerStreamListeners.clone();
		LVSPlayerStreamEvent lvsPlayerStreamEvent = new LVSPlayerStreamEvent(
				this);
		int size = playerStreamListeners.size();
		for (int i = 0; i < size; i++) {
			((LVSPlayerStreamListener) currentListeners.elementAt(i))
					.participantLeaveEvent(lvsPlayerStreamEvent);
		}
	}*/

}